package cn.mayday.algorithms.year2021.month1.A普通业务处理;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * 面向socket编程，请用代码实现2个用户间互say hello的逻辑
 *
 * @author Mayday05
 * @date 2021/5/13 15:33
 */
public class Wuzong4 {
//    https://www.cnblogs.com/mingforyou/p/3258418.html



    public static void main(String[] args) {

        new Thread(new Runnable() {

            @Override
            public void run() {
                startServer();
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                startClient();
            }
        }).start();

    }

    public static void startClient() {
        try {
            // 向本机的4700端口发出客户请求
            Socket socket = new Socket("127.0.0.1", 4700);
            System.out.println("start client .....");

            // 由Socket对象得到输出流，并构造PrintWriter对象
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            //将从系统标准输入读入的字符串输出到Server
            os.println("hello, server...");
            //刷新输出流，使Server马上收到该字符串
            os.flush();

            // 由Socket对象得到输入流，并构造相应的BufferedReader对象
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 在标准输出上打印从客户端读入的字符串
            System.out.println("Receive Server Information : " + is.readLine());

            while (!("hello, client...").equals(is.readLine())) {
                System.out.println("wait......");
                Thread.sleep(1000);
                break;
            }


            os.close(); //关闭Socket输出流
            socket.close(); //关闭Socket

            System.out.println("stop client .....");

        } catch (Exception e) {
            e.printStackTrace();//出错，打印出错信息
        }
    }

    public static void startServer() {
        try {
            ServerSocket server = null;
            try {
                //创建一个ServerSocket在端口4700监听客户请求
                server = new ServerSocket(4700);

                System.out.println("start server .....");
            } catch (Exception e) {
                e.printStackTrace();//出错，打印出错信息
            }
            Socket socket = null;
            try {
                //使用accept()阻塞等待客户请求，有客户
                socket = server.accept();//请求到来则产生一个Socket对象，并继续执行
            } catch (Exception e) {
                e.printStackTrace();//出错，打印出错信息
            }
            // 由Socket对象得到输入流，并构造相应的BufferedReader对象
            BufferedReader is = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            // 在标准输出上打印从客户端读入的字符串
            System.out.println("Receive Client Information : " + is.readLine());

            // 由Socket对象得到输出流，并构造PrintWriter对象
            PrintWriter os = new PrintWriter(socket.getOutputStream());
            //将从系统标准输入读入的字符串输出到Server
            os.println("hello, client...");

            //刷新输出流，使Client马上收到该字符串
            os.flush();

            os.close(); //关闭Socket输出流
            is.close(); //关闭Socket输入流
            socket.close(); //关闭Socket
            server.close(); //关闭ServerSocket

            System.out.println("stop server .....");

        } catch (Exception e) {
            e.printStackTrace();//出错，打印出错信息
        }
    }
}
